package preload
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.geom.Matrix;
	import flash.system.Capabilities;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	import mx.events.FlexEvent;
	import mx.events.RSLEvent;
	import mx.preloaders.SparkDownloadProgressBar;
	
	public class PreloadPage extends SparkDownloadProgressBar
	{       
		[Embed(source="resources/image/loading.jpg", mimeType="application/octet-stream")]
		private var _loadingLogo:Class;
		private var dpbImageControl:Loader;
		private var progressText:TextField; 
		private var _barSprite:Sprite; 
		
		private var screenX:int = Capabilities.screenResolutionX;
		private var screenY:int = Capabilities.screenResolutionY;
		
		private var rslBaseText:String = "开始加载中：";
		private var errorLabel:TextField;
		private var errorNav:TextField;
		private var _displayStartCount:uint = 0;
		private var _initProgressCount:uint = 0;
		private var _downloadComplete:Boolean = false;
		private var _showingDisplay:Boolean = false;
		private var _startTime:int;
		
		public function PreloadPage() {
			super();
		}
		
		/**
		 *  Event listener for the <code>FlexEvent.INIT_COMPLETE</code> event.
		 *  NOTE: This event can be commented out to stop preloader from completing during testing
		 */
		override protected function initCompleteHandler(event:Event):void {
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		/**
		 *  Creates the subcomponents of the display.
		 */
		override protected function createChildren():void {
			if(!dpbImageControl){
/*				dpbImageControl = new Loader(); 
				dpbImageControl.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_completeHandler);
				dpbImageControl.loadBytes(new _loadingLogo() as ByteArray);*/
				
				drawSprite();
			}
		}
		
		// After the SWF file loads, set the size of the Loader control.
		private function loader_completeHandler(event:Event):void {   
			dpbImageControl.width = 235;
			dpbImageControl.height= 176;
			dpbImageControl.x = screenX/2 - 235/2;
			dpbImageControl.y = 165;
			addChild(dpbImageControl);
		}
		
		private function drawSprite():void{
			//绘制背景1
			var matrix:Matrix=new Matrix();
			matrix.createGradientBox(this.stage.stageWidth,this.stage.stageHeight*2,Math.PI/2);
			var colors:Array=[0x1F66B6,0x1F66B6] ;
			var alphas:Array=[1,1];
			var ratios:Array=[0,255];
			this.graphics.lineStyle();
			this.graphics.beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,matrix);       
			this.graphics.drawRect(0,0,this.stage.stageWidth,this.stage.stageHeight);
			this.graphics.endFill();
			
			//绘制背景2
			var _matrix:Matrix=new Matrix();
			_matrix.createGradientBox(1028,768,Math.PI/2);
			var _colors:Array=[0x4B93DF,0x1F66B6];
			var _alphas:Array=[1,1];
			var _ratios:Array=[0,255];
			this.graphics.lineStyle();
			this.graphics.beginGradientFill(GradientType.LINEAR,_colors,_alphas,_ratios,_matrix);       
			this.graphics.drawRect(0,0,this.stage.stageWidth,768);
			this.graphics.endFill(); 
			
			//-------------------------------------------------
			//绘制进度条背景
			var _Sprite2:Sprite = new Sprite();
			addChild(_Sprite2);
			_Sprite2.graphics.lineStyle(1, 0xCCCCCC);
			_Sprite2.graphics.beginFill(0xFFFFFF);
			_Sprite2.graphics.drawRect((screenX/2 - 152), (screenY/2 - 170), 303, 13);  
			_Sprite2.graphics.endFill();
			
			//-------------------------------------------------
			var _Sprite3:Sprite = new Sprite();
			addChild(_Sprite3);
			_Sprite3.graphics.lineStyle(1, 0xCCCCCC);
			_Sprite3.graphics.beginFill(0xCCCCCC);
			_Sprite3.graphics.drawRect((screenX/2 - 152), (screenY/2 - 170), 303, 6);  
			_Sprite3.graphics.endFill();
			
			//加载进度条Sprite
			_barSprite = new Sprite();
			_barSprite.x = screenX/2 - 150;
			_barSprite.y = screenY/2 - 170 +2;
			addChild(_barSprite);
			
			progressText = new TextField();
			progressText.textColor = 0xffffff;
			progressText.width = 300;
			progressText.height = 18;
			progressText.x = screenX/2 - 52;
			progressText.y = screenY/2 - 150;
			addChild(progressText);
			
			errorLabel=new TextField();
			errorLabel.textColor = 0xffffff;
			errorLabel.height = 18;
			errorLabel.text="如果页面显示异常，请点击";
			errorLabel.width=150;
			errorLabel.x = Capabilities.screenResolutionX/2 - 100;
			errorLabel.y = Capabilities.screenResolutionY/2 - 120;
			addChild(errorLabel);
			
			errorNav=new TextField();
			errorNav.textColor = 0x66FF00;
			errorNav.height = 18;
			errorNav.htmlText="<a href='http://122.224.88.34:8083/gpsYj/pro.jsp' target='_blank'>解决方案</a>";
			errorNav.x = Capabilities.screenResolutionX/2+50;
			errorNav.y = Capabilities.screenResolutionY/2 - 120;
			addChild(errorNav);
		}
		
		/**
		 * Event listener for the <code>ProgressEvent.PROGRESS event</code> event.
		 * Download of the first SWF app
		 **/
		override protected function progressHandler(evt:ProgressEvent):void {
			if (_barSprite != null && progressText != null){
				var progressApp:Number = Math.round((evt.bytesLoaded/evt.bytesTotal)*100);
				setPreloaderLoadingText(rslBaseText + progressApp + "%");
				drawProgressBar(evt.bytesLoaded,evt.bytesTotal);
			}else{
				show();
			}
		}
		
		//刷新进度条
		private function drawProgressBar(bytesLoaded:Number, bytesTotal:Number):void {  
			if (_barSprite != null){
				var g:Graphics = _barSprite.graphics;
				g.clear();
				var matrix:Matrix=new Matrix();
				matrix.createGradientBox(300*(bytesLoaded/bytesTotal),10,Math.PI/2);
				var colors:Array=[0x5499E6,0x64B0EA];
				var alphas:Array=[1,1];
				var ratios:Array=[0,255];
				g.lineStyle();
				g.beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,matrix);       
				g.drawRect(0,0,300*(bytesLoaded/bytesTotal),10);
				g.endFill(); 
			}
		}
		/**
		 * Event listener for the <code>RSLEvent.RSL_PROGRESS</code> event.
		 **/
		override protected function rslProgressHandler(evt:RSLEvent):void {
			if (evt.rslIndex && evt.rslTotal) {
				var progressRsl:Number = Math.round((evt.bytesLoaded/evt.bytesTotal)*100);
				rslBaseText = "加载共享库：";
				if (_barSprite && progressText){
					setPreloaderLoadingText(rslBaseText + progressRsl + "%");
					drawProgressBar(evt.bytesLoaded,evt.bytesTotal);
				}
			}
		}
		
		/**
		 *  indicate download progress.
		 */
		override protected function setDownloadProgress(completed:Number, total:Number):void {
		}
		
		/**
		 *  Updates the inner portion of the download progress bar to
		 *  indicate initialization progress.
		 */
		override protected function setInitProgress(completed:Number, total:Number):void {
			if (_barSprite && progressText){
				if (completed > total) {
					setPreloaderLoadingText("加载完成");
					drawProgressBar(1,1);
				} else {
					setPreloaderLoadingText("初始化应用：" + Math.round((completed/total)*100) + "%");
					drawProgressBar(completed,total);
				}
			}
		} 
		
		/**
		 *  Event listener for the <code>FlexEvent.INIT_PROGRESS</code> event.
		 *  This implementation updates the progress bar
		 *  each time the event is dispatched.
		 */
		override protected function initProgressHandler(event:Event):void {
			var elapsedTime:int = getTimer() - _startTime;
			_initProgressCount++;
			
			if (!_showingDisplay &&	showDisplayForInit(elapsedTime, _initProgressCount)) {
				_displayStartCount = _initProgressCount;
				show();
			}
			
			if (_showingDisplay) {
				setInitProgress(_initProgressCount, initProgressTotal);
			}
		}
		
		private function show():void {
			if (stageWidth == 0 && stageHeight == 0) {
				try {
					stageWidth = stage.stageWidth;
					stageHeight = stage.stageHeight
				} catch (e:Error) {
					stageWidth = loaderInfo.width;
					stageHeight = loaderInfo.height;
				}
				if (stageWidth == 0 && stageHeight == 0)
					return;
			}
			
			_showingDisplay = true;
			createChildren();
		}
		
		private function setPreloaderLoadingText(value:String):void {
			progressText.text = value;
		}
	}
	
}
